﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using com.pro2e.console.service.auth.dao;
using System.Collections.Generic;
using com.pro2e.console.service.auth.vo;
using com.pro2e.web.treeview;
using com.pro2e.console.service.auth;
using com.pro2e.web.util.page;
using com.pro2e.console.core.util;
using System.IO;
using com.pro2e.web.config;
using com.pro2e.web.util;
public partial class admin_leftmenu : System.Web.UI.Page
{
    log4net.ILog log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
    SessionHelper sHelper = new SessionHelper();
    AuthService authService = new AuthService();
    ConfigHelper configHelper = new ConfigHelper();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            initMenu();

            new TreeViewState().RestoreTreeView(tvMenu, this.GetType().ToString());
            //初始選單下方左邊資料
            initLeftData();

        }
    }

    private void initMenu()
    {
        LoginUser user = sHelper.LoginUser;
        //快取載入
        UserMenuFuncContainer userContainer = UserMenuFuncContainer.GetInstance();
        if (user == null)
        {
            Response.Redirect(UIHelper.LOGIN_PAGE_MANAGER, false);
            return;
        }
        // user = userContainer.GetUser(user.UserId);

        TreeveiwService tvService = new TreeveiwService();

        MenuFuncDao menuFuncDao = new MenuFuncDao();

        IList<MenuFunc> menuFuncList = authService.GetTopMenuFunc(user, userContainer.AllMenu, userContainer.RoleDic);

        foreach (MenuFunc menu in menuFuncList)
        {
            TreeNode treeNode = new TreeNode(menu.MenuFuncName, menu.Id.ToString(), "", "welcome.aspx", "mainfrm");

            if (menu.SubFuncs.Count > 0)
            {
                foreach (MenuFunc subMenu in menu.SubFuncs)
                {
                    if (subMenu.Note != null)
                    {
                        if (subMenu.Note.ToLower().IndexOf("_sub") == -1)
                        {
                            TreeNode subTreeNode = new TreeNode(subMenu.MenuFuncName, null, null, "~/" + subMenu.MainPath, "mainfrm");
                            treeNode.ChildNodes.Add(subTreeNode);
                        }
                    }
                }
            }

            tvMenu.Nodes.Add(treeNode);

        }
        tvMenu.Nodes[0].Expand();
        tvMenu.DataBind();
    }
    protected void tvMenu_SelectedNodeChanged(object sender, EventArgs e)
    {
  
        if (tvMenu.SelectedNode.Value != string.Empty)
        {
            Response.Redirect("~/" + tvMenu.SelectedNode.Value, false);
        }
    }
    protected void tvMenu_Unload(object sender, EventArgs e)
    {
        //  log.Debug("enter tvMenu_Unload");
        new TreeViewState().SaveTreeView(tvMenu, this.GetType().ToString());
    }


    private void initLeftData()
    {
        LoginUser user = sHelper.LoginUser;

        if (user != null)
        {
        
            //快取載入
            // user = UserMenuFuncContainer.GetInstance().GetUser(user.UserId);

            user = authService.LoadLoginUser(user.UserId);

            IList<LoginRole> roleList = user.BelongRoles;

            List<string> roleStr = new List<string>();

            //if (roleList != null && roleList.Count > 0)
            //{
            //    foreach (LoginRole role in roleList)
            //    {
            //        roleStr.Add(role.RoleName);
            //    }
            //}

            //lblRole.Text = String.Join(",", roleStr.ToArray());
        }
        else
        {
            Response.Redirect(UIHelper.LOGIN_PAGE_MANAGER, false);
            return;
        }
    }
    protected void btnLogout_Click(object sender, ImageClickEventArgs e)
    {
        sHelper.LoginUser = null;

        Response.Redirect(UIHelper.LOGIN_PAGE_MANAGER, false);
        return;
    }
    protected void btnLogout_Click1(object sender, EventArgs e)
    {

        logout();
    }
    protected void btnLogout_Click1(object sender, ImageClickEventArgs e)
    {
        logout();
    }
    private void logout()
    {
        sHelper.LoginUser = null;
        Session.Clear();
        string url = string.Format("top.document.location.href='http://{0}';", configHelper.Host);
        return;
    }

}
